package com.lrkj.property.service.impl;

import com.lrkj.common.annotation.DataScope;
import com.lrkj.common.constant.OrgConstants;
import com.lrkj.common.core.domain.entity.OrgTree;
import com.lrkj.common.exception.BaseException;
import com.lrkj.common.utils.DateUtils;
import com.lrkj.common.utils.SecurityUtils;
import com.lrkj.common.utils.uuid.IdUtils;
import com.lrkj.property.domain.Community;
import com.lrkj.property.domain.Floor;
import com.lrkj.property.domain.Unit;
import com.lrkj.property.mapper.CommunityMapper;
import com.lrkj.property.service.ICommunityService;
import com.lrkj.property.service.IFloorService;
import com.lrkj.property.vo.CommunityParkingVo;
import com.lrkj.system.service.IOrgTreeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 小区信息Service业务层处理
 *
 * @author yajun_ren
 * @date 2021-07-22
 */
@Service
public class CommunityServiceImpl implements ICommunityService {
    @Autowired
    private CommunityMapper communityMapper;

    @Autowired
    private IOrgTreeService orgTreeService;

    @Autowired
    private IFloorService floorService;

    /**
     * 查询小区信息
     *
     * @param id 小区信息ID
     * @return 小区信息
     */
    @Override
    public Community selectCommunityById(String id) {
        return communityMapper.selectCommunityById(id);
    }

    /**
     * 查询小区信息列表
     *
     * @param community 小区信息
     * @return 小区信息
     */
    @Override
    @DataScope(deptAlias = "t")
    public List<Community> selectCommunityList(Community community) {
        return communityMapper.selectCommunityList(community);
    }

    /**
     * 新增小区信息
     *
     * @param community 小区信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertCommunity(Community community) {
        community.setId(IdUtils.simpleUUID());
        community.setCreateBy(SecurityUtils.getUsername());
        community.setCreateTime(DateUtils.getNowDate());
        this.setUnitInfo(community);
        return communityMapper.insertCommunity(community);
    }

    /**
     * 修改小区信息
     *
     * @param community 小区信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateCommunity(Community community) {
        this.setUnitInfo(community);
        community.setUpdateBy(SecurityUtils.getUsername());
        community.setUpdateTime(DateUtils.getNowDate());
        return communityMapper.updateCommunity(community);
    }


    private void setUnitInfo(Community bean) {
        OrgTree parentBean = orgTreeService.selectOrgTreeById(bean.getDeptId());
        bean.setAncestors(parentBean.getAncestors() + "," + bean.getDeptId());
        orgTreeService.insertOrgTree(bean.getId(), bean.getCommunityName(), bean.getDeptId(), OrgConstants.community, bean.getAncestors());
    }

    /**
     * 批量删除小区信息
     *
     * @param ids 需要删除的小区信息ID
     * @return 结果
     */
    @Override
    public int deleteCommunityByIds(String[] ids) {
        for(String id : ids){
            this.deleteVerify(id);
        }
        orgTreeService.deleteOrgTreeByIds(ids);
        return communityMapper.deleteCommunityByIds(ids);
    }

    /**
     * 删除小区信息信息
     *
     * @param id 小区信息ID
     * @return 结果
     */
    @Override
    public int deleteCommunityById(String id) {
        this.deleteVerify(id);
        orgTreeService.deleteOrgTreeById(id);
        return communityMapper.deleteCommunityById(id);
    }

    @Override
    public int fiscalPeriod(Community community) {
        return communityMapper.updateCommunity(community);
    }

    @Override
    public CommunityParkingVo getCommunityParking(String id) {
        return communityMapper.getCommunityParking(id);
    }

    @Override
    public Map<String, Object> getCommunitySite(String id) {
        return communityMapper.getCommunitySite(id);
    }

    @Override
    public Map<String, Object> getCommunityAdvert(String id) {
        return communityMapper.getCommunityAdvert(id);
    }

    @Override
    public int initFiscalPeriod(Community community) {
        return communityMapper.initFiscalPeriod(community);
    }


    /***
     * 删除小区信息,校验关联信息
     *
     * **/
    private void deleteVerify(String communityId) {
        Floor eg = new Floor();
        eg.setCommunityId(communityId);
        int count = floorService.selectCount(eg);
        if (count > 0) {
            throw new BaseException("删除小区失败,请先删除关联的楼栋信息!");
        }
    }
}
